package com.xt.LibaryDeskManager.servlet;


import com.xt.LibaryDeskManager.dto.UserMessage;
import com.xt.LibaryDeskManager.service.AdminService;
import com.xt.LibaryDeskManager.service.StudentService;
import com.xt.LibaryDeskManager.service.TeacherService;
import com.xt.LibaryDeskManager.service.impl.AdminServiceImpl;
import com.xt.LibaryDeskManager.service.impl.StudentServiceImpl;
import com.xt.LibaryDeskManager.service.impl.TeacherServiceImpl;
import com.xt.LibaryDeskManager.utils.StrUtils;
import com.xt.LibaryDeskManager.vo.LoginVo;
import com.xt.LibaryDeskManager.vo.StudentVo;
import com.xt.LibaryDeskManager.vo.UpdateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    /**
     * 学生，教师和管理员登录
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/doLogin")
    public Map doLogin(LoginVo loginVo, HttpSession session)throws Exception{
        int code=0;
        UserMessage userMessage=new UserMessage();
            if (loginVo.getLevel()==3){
                //管理员登录
                userMessage=adminService.selectByLoginVo(loginVo);
                code=3;
            }else if (loginVo.getLevel()==2){
                //教师登录
                userMessage=teacherService.selectByLoginVo(loginVo);
                code=2;
            }else{
                //学生登录
                userMessage=studentService.selectByLoginVo(loginVo);
                code=1;
                if (userMessage!=null&&!userMessage.getValid()){
                    code=4;
                }
            }
        if (userMessage!=null&&userMessage.getLevel()!=0){
            //记住我功能
            session.setAttribute("user",userMessage);
        }else{
            code=0;
        }
      return StrUtils.creatMessage("code",code);
    }

    /**
     * 学生，教师和管理员退出功能
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/doOut")
    public String doOut(HttpSession session){
        session.removeAttribute("user");
        return "redirect:../index.jsp";
    }
    /**
     * 学生教师管理员修改密码功能
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/doupdatePwd")
    public Map doupdatePwd(HttpSession session,String newpassword) throws Exception {
        UserMessage userMessage= (UserMessage) session.getAttribute("user");
        UpdateVo updateVo=new UpdateVo(userMessage.getId(),newpassword,userMessage.getPhone(),userMessage.getImg(),userMessage.getValid());
        int code=0;
        if (userMessage.getLevel()==1){
            //学生修改密码

            code=studentService.updateStudent(updateVo);
        }else if (userMessage.getLevel()==2){
            //教师修改密码

            code=teacherService.updateTeacher(updateVo);
        }else if (userMessage.getLevel()==3){
            //管理员修改密码
            code=adminService.updateAdmin(updateVo);
        }
        return StrUtils.creatMessage("code",code);
    }
    /**
     * 更改信息
     */
    @ResponseBody
    @RequestMapping("/doupdateMessage")
    public Map doupdateMessage(HttpServletRequest req,HttpSession session, MultipartFile img1, String phone)throws Exception{
        UserMessage userMessage= (UserMessage) session.getAttribute("user");
        String fileName = UUID.randomUUID() + "header.jpg";
        String path = req.getServletContext().getRealPath("img");
        if (img1.getSize()!=0){
            img1.transferTo(new File(path + "/" + fileName));
        }
        UpdateVo updateVo=new UpdateVo(userMessage.getId(),userMessage.getPassword(),phone,"/img/"+fileName,userMessage.getValid());
        int code=0;
        if (userMessage.getLevel()==1){
            //学生修改信息
            code=studentService.updateStudent(updateVo);
        }else if (userMessage.getLevel()==2){
            //教师修改信息
            code=teacherService.updateTeacher(updateVo);
        }else if (userMessage.getLevel()==3){
            //管理员修改信息
            code=adminService.updateAdmin(updateVo);
        }
        if (code!=0){
            UserMessage userMessage1= (UserMessage) session.getAttribute("user");
            userMessage1.setPhone(phone);
            userMessage1.setImg("/img/"+fileName);
            session.setAttribute("user",userMessage1);
        }
        return StrUtils.creatMessage("code",code);
    }

    /**
     * 校验用户是否存在
     */
    @ResponseBody
    @RequestMapping("/ExamineStudent")
    protected Map ExamineStudent(String studentno) throws Exception {
        System.out.println(studentno);
        int code=0;
        UserMessage userMessage=studentService.selectByNo(studentno);
        if (userMessage!=null){
            code=1;
        }
        return StrUtils.creatMessage("code",code);
    }

    /**
     * 用户注册
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/register")
    public Map register(StudentVo studentVo,HttpSession session) throws Exception {
        UserMessage userMessage= (UserMessage) session.getAttribute("user");
        int level=1;
        if (userMessage!=null){
           level=userMessage.getLevel();
        }
        int code= studentService.addStudent(studentVo);
        Map map=new HashMap();
        map.put("code",code);
        map.put("level",level);
        return map;
    }

}
